package com.ruoyi.develop.finance.financeBuyInvoiceStoreRel.service.impl;

import java.math.BigDecimal;
import java.util.List;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.uuid.IdUtils;

import javax.annotation.Resource;

import com.ruoyi.constants.BusinessConstants;
import com.ruoyi.develop.lvRukumingxi.domain.LvRukumingxi;
import com.ruoyi.develop.lvRukumingxi.mapper.LvRukumingxiMapper;
import org.springframework.stereotype.Service;
import com.ruoyi.develop.finance.financeBuyInvoiceStoreRel.mapper.FinanceBuyInvoiceStoreRelMapper;
import com.ruoyi.develop.finance.financeBuyInvoiceStoreRel.domain.FinanceBuyInvoiceStoreRel;
import com.ruoyi.develop.finance.financeBuyInvoiceStoreRel.service.IFinanceBuyInvoiceStoreRelService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 采购发票和入库明细关系Service业务层处理
 *
 * @author lmm
 * @date 2025-07-25
 */
@Service
public class FinanceBuyInvoiceStoreRelServiceImpl implements IFinanceBuyInvoiceStoreRelService {
    @Resource
    private FinanceBuyInvoiceStoreRelMapper financeBuyInvoiceStoreRelMapper;

    @Resource
    private LvRukumingxiMapper lvRukumingxiMapper;


    /**
     * 查询采购发票和入库明细关系
     *
     * @param id 采购发票和入库明细关系主键
     * @return 采购发票和入库明细关系
     */
    @Override
    public FinanceBuyInvoiceStoreRel selectById(String id) {
        return financeBuyInvoiceStoreRelMapper.selectById(id);
    }

    /**
     * 查询采购发票和入库明细关系列表
     *
     * @param entity 采购发票和入库明细关系
     * @return 采购发票和入库明细关系
     */
    @Override
    public List<FinanceBuyInvoiceStoreRel> selectList(FinanceBuyInvoiceStoreRel entity) {
        return financeBuyInvoiceStoreRelMapper.selectList(entity);
    }

    /**
     * 新增采购发票和入库明细关系
     *
     * @param entity 采购发票和入库明细关系
     * @return 结果
     */
    @Override
    public int insert(FinanceBuyInvoiceStoreRel entity) {
        if (StringUtils.isBlank(entity.getId())) {
            entity.setId(IdUtils.fastSimpleUUID());
        }
        entity.setCreateBy(SecurityUtils.getUsername());
        entity.setCreateTime(DateUtils.getNowDate());
        return financeBuyInvoiceStoreRelMapper.insert(entity);
    }

    /**
     * 修改采购发票和入库明细关系
     *
     * @param entity 采购发票和入库明细关系
     * @return 结果
     */
    @Override
    public int update(FinanceBuyInvoiceStoreRel entity) {
        entity.setUpdateBy(SecurityUtils.getUsername());
        entity.setUpdateTime(DateUtils.getNowDate());
        return financeBuyInvoiceStoreRelMapper.update(entity);
    }

    /**
     * 批量删除采购发票和入库明细关系
     *
     * @param ids 需要删除的采购发票和入库明细关系主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteByIds(String[] ids) {
        for (String id: ids) {
            updateRukumingxi(id);
        }
        return financeBuyInvoiceStoreRelMapper.deleteByIds(ids);
    }

    /**
     * 删除采购发票和入库明细关系信息
     *
     * @param id 采购发票和入库明细关系主键
     * @return 结果
     */
    @Override
    public int deleteById(String id) {
        updateRukumingxi(id);
        return financeBuyInvoiceStoreRelMapper.deleteById(id);
    }

    public void updateRukumingxi(String relId){
        FinanceBuyInvoiceStoreRel entity = financeBuyInvoiceStoreRelMapper.selectById(relId);
        LvRukumingxi lvRukumingxi = lvRukumingxiMapper.selectById(entity.getStoreDetailId());
        lvRukumingxi.setInvoiceRemain(lvRukumingxi.getInvoiceRemain().add(entity.getAmount()));
        lvRukumingxi.setInvoiceAmount(lvRukumingxi.getInvoiceAmount().subtract(entity.getAmount()));
        if(lvRukumingxi.getInvoiceAmount().compareTo(BigDecimal.ZERO) == 0){
            lvRukumingxi.setInvoiceStatus(BusinessConstants.TAX_STATUS_0);
        }
        if(lvRukumingxi.getInvoiceRemain().compareTo(BigDecimal.ZERO) > 0){
            lvRukumingxi.setInvoiceStatus(BusinessConstants.TAX_STATUS_1);
        }
        lvRukumingxiMapper.update(lvRukumingxi);
    }

    @Override
    public FinanceBuyInvoiceStoreRel selectNotCommit(String invoiceId, String rukumingxiId, String goodsId, String createBy, String status) {
        return financeBuyInvoiceStoreRelMapper.selectNotCommit(invoiceId, rukumingxiId, goodsId, createBy, status);
    }


}
